package com.cos.web.interceptor;

import com.cos.entity.User;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @author: COS
 * @time: 2021/11/17 15:09
 * @description: 拦截器和过滤器区别
 * 1.实现方式不一样
 * 拦截器 反射方式完成      过滤器 方法回调
 * 2.拦截目标不同
 * 拦截器 action/handler(方法级) 过滤器 类级别 servlet html js css
 * 3.可以获取的数据不一样
 * 拦截器可以输入属性的值     过滤器无法注入属性的值
 * 4.运行顺序
 * 拦截器 在过滤器后执行,过滤器先执行再到拦截器
 */
public class MyInterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        System.out.println("拦截器进入之前");
        User is_login = (User) request.getSession().getAttribute("is_login");
        if (is_login == null) {
            response.sendRedirect("/jsp/login.jsp");
            return false;
        }
//        return false;//拦截
        return true;//放行
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
        System.out.println("拦截器返回");
    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
        //资源释放使用
    }
}
